﻿
CREATE PROCEDURE [dbo].[proc_Project_Task_Delete]
	(
		@TaskId uniqueidentifier
	)
AS
Begin
	--处理整个项目
	Declare @DSum int, @DFinishSum int, @ProjectId uniqueidentifier
	Set @ProjectId = Null 
	Select @ProjectId=ProjectId From Project_Task Where TaskId=@TaskId
	If @ProjectId Is null
	Begin
		Select -1 As ReturnValue
		Return
	End
	
	--删除该任务
	Delete From Project_Task Where TaskId=@TaskId

	--取删除后该项目所有任务(含部门和个人)
	Select @DSum=Count(0) From Project_Task Where ProjectId=@ProjectId
	If @DSum=0	--这个项目就这1个任务
	Begin
		Select 1 As ReturnValue
		Return
	End
	
	--取该项目所有未完成的任务(含部门和个人)
	Select @DFinishSum=Count(0) From Project_Task Where ProjectId=@ProjectId And IsFinish=0
	If(@DFinishSum=0)	--所有的任务都完成了
		Update Project Set IsFinish=1,FinishDate=getdate() Where ProjectId=@ProjectId And ProductId Not In(select id from Product where flag=1)	--更新项目为完结(网站项目除外)

	Select 1 As ReturnValue
End